import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { CTableProps } from '@/components/CTable'
import type { BasicPagingListParams } from '@/apis/basicModel'
import type { SearchOptionsType, TableFieldsSettingType } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'
import { getMonthlyEnergyList } from '@/apis/taa/target/monitoring'
import { getCurrentOrganizationListTenant } from '@/apis/common/common'

export const useTableList = () => {
  const searchForm = reactive({
    organizationCode: '',
    dataYear: ''
  })
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '工厂',
      placeholder: '工厂',
      field: 'organizationCode',
      type: 'select',
      remoteMethod: (query: string): Promise<any> => {
        const params = {
          organizationName: query
        }
        return getCurrentOrganizationListTenant(params).then((res) => {
          const ans = res.map((item) => {
            return {
              label: item.organizationName,
              value: item.organizationCode
            }
          })
          return ans
        })
      }
    },
    {
      name: '年月',
      field: 'dataYear',
      type: 'datePicker',
      dateType: 'month',
      format: 'YYYY-MM'
    }
  ])

  // 表格配置
  const tableOptions: CTableProps = {
    loadData
  }

  function loadData(parameter: BasicPagingListParams) {
    const params = Object.assign(handleParams(), parameter)
    return getMonthlyEnergyList(params)
  }

  // 处理参数
  const handleParams = () => {
    // 参数
    let params = {
      organizationCode: searchForm.organizationCode,
      dataYear: searchForm.dataYear?.split('-')?.[0],
      dataMonth: searchForm.dataYear?.split('-')?.[1]
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '工厂名称/工厂编号',
      field: 'organizationName',
      slot: 'organizationName',
      minWidth: 130,
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.REQUIRED
      }
    },
    {
      title: '年月',
      field: 'dataYear',
      slot: 'dataYear',
      minWidth: 90,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '总用电量（kWh）',
      field: 'electricAmt',
      minWidth: 130,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '总用气量（m³）',
      field: 'gasAmt',
      minWidth: 150,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '总用水量（m³）',
      field: 'waterAmt',
      minWidth: 150,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '碳排放总量（t）',
      field: 'totalEmission',
      minWidth: 150,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 160,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  return {
    searchForm,
    searchOptions,
    tableOptions,
    tableFieldsSetting
  }
}
